package org.dreamwork.hyacinth.jasmine;

import org.dreamwork.hyacinth.HexAttachment;
import org.dreamwork.jasmine2.engine.HttpContext;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.peony.DataBinder;
import org.dreamwork.util.StringUtil;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 12-3-20
 * Time: 下午6:23
 */
public class InlineImage extends HTMLMailControl {
    private String src;
    private String contentTypeField;

    public InlineImage () {
        addValidateListener (this);
        this.htmlTagName = "img";
    }

    public String getSrc () {
        return src;
    }

    public void setSrc (String src) {
        this.src = src;
    }

    public String getContentTypeField () {
        return contentTypeField;
    }

    public void setContentTypeField (String contentTypeField) {
        this.contentTypeField = contentTypeField;
    }

    @Override
    public void onDataBind (Object sender) throws EventException {
        IImageCreator creator = null;
        if (dataSource != null) {
            if (StringUtil.isEmpty (valueField) || StringUtil.isEmpty (contentTypeField))
                throw new IllegalArgumentException (
                        StringUtil.isEmpty (valueField) ? "valueField" : "contentTypeField"
                );
            Object data = DataBinder.eval (dataSource, valueField);
            if (!(data instanceof byte[]))
                throw new IllegalStateException ("illegal image data");
            String contentType = (String) DataBinder.eval (dataSource, contentTypeField);
            creator = new DataSourceImageCreator ((byte[]) data, contentType);
        } else if (!StringUtil.isEmpty (src)) {
            try {
                String s = src.toLowerCase ();
                URL url;
                if (s.startsWith ("http://"))
                    url = new URL (src);
                else {
                    ServletContext application = context.getApplication ();
                    if (src.charAt (0) != '/') src = '/' + src;
                    url = application.getResource (src);
                }
                creator = new URLImageCreator (url);
            } catch (IOException ex) {
                throw new EventException (ex);
            }
        }
        if (creator != null) {
            HexAttachment attachment = new HexAttachment (creator.getImageData (), creator.getContentType ());
            if(src == null){
                String contentType = (String)DataBinder.eval (dataSource, contentTypeField);
                String [] type = contentType.split("/");
                attachment.setFileName(this.getId() + "." + type[1]);
            } else {
                int pos = src.lastIndexOf ('/');
                if (pos != -1)
                    attachment.setFileName (src.substring (pos + 1));
            }

            ((HTMLMail) page).addAttachment (id, attachment);
        }
    }

    @Override
    public void onPrerender (IWebControl sender, HttpContext context) throws EventException {
        setAttribute ("src", "cid:" + id);
        super.onPrerender (sender, context);
    }
}